/**
 * 
 */
package com.p2p.front.task;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.p2p.front.dal.model.LoanInfo;
import com.p2p.front.dal.model.LoanVariation;
import com.p2p.front.service.LoanInfoService;
import com.p2p.front.service.LoanVariationService;
import com.p2p.front.tender.FlowLoanTender;
import com.p2p.front.tender.NormalLoanTender;

/**
 * @author ruiqi
 *
 */
@Service("loanTimeOutTask")
public class LoanTimeOutTask extends BaseTask {

	@Resource
	private LoanInfoService loanInfoService;
	
	@Resource
	private LoanVariationService loanVariationService;
	
	@Resource
	private FlowLoanTender flowLoanTender;

	@Resource
	private NormalLoanTender normalLoanTender;
	
	@Scheduled( cron="0 0/1 * * * ? ")///每分钟一次
	public void findTimeOutLoanSchedule(){
		if(!this.doTaskSynchronous(getBizKey(), 20)){
			return;
		}
		try {
			doBiz();
		} catch (Exception e) {
			logger.error("",e);
		}
		this.doRmoveTaskSynchronous(getBizKey());
	}

	@Override
	public String getBizKey() {
		String bizKey = "task_loanTimeOutSchedule_";
		return bizKey;
	}
	
	public void doBiz(){
		logger.info("start scan expired loan");
		List<LoanVariation> list = loanVariationService.findExpiredLoan();
		logger.info("find "+list.size()+" expired loan");
		for(LoanVariation loanVariation:list){
			LoanInfo loanInfo = loanVariationService.getLoanInfoFromCache(loanVariation.getLoanId());
			
			if(loanInfo.getIsFlow()!=null&&loanInfo.getIsFlow()==1){
				flowLoanTender.doTenderExpiredBiz(loanVariation);
			}else{
				normalLoanTender.doTenderExpiredBiz(loanVariation);
			}
			logger.info("end do TenderExpiredBiz, loanId="+loanVariation.getLoanId());
			
		}
	}
	
}
